package stack;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;

public class isValid {
    public boolean isValid(String s) {
        Deque<Character> queue = new ArrayDeque<>();
        HashMap<Character, Character> map = new HashMap();
        map.put(')', '(');
        map.put(']', '[');
        map.put('}', '{');
        for (Character c : s.toCharArray()) {

            if (c == '(' || c == '[' || c == '{') {
                queue.push(c);
            } else {

                if (queue.peek() == null) {
                    // 相当于上来就是右符号
                    return false;
                }
                Character pop = queue.pop();
                Character item = map.get(c);
                if (pop != item) {
                    return false;
                }
            }
        }

        return queue.isEmpty();
    }

    public static void main(String[] args) {
        isValid obj = new isValid();
        // String s = "()[]{}";
        // String s = "([])";
        // String s = "([)]";
        String s = "]()[[]";
        System.out.println(obj.isValid(s));
    }
}
